package com.linked_list;

import com.company.ArrayStack;
import com.company.Stack;

import java.util.Random;

/**
 * 性能测试: 基于数组实现的栈 和 基于链表实现的栈  的性能
 *
 * 结论：这两者的时间复杂度是一样的，具体的和操作数的大小还有jvm的版本有关
 */
public class Main {
    public static void main(String[] args) {
        int opCount = 10000000;

        ArrayStack<Integer> arrayStack = new ArrayStack<>();
        double t1 = testStack(arrayStack, opCount);
        System.out.println("arrayStack , time: " + t1 + " s");

        LinkedListStack<Integer> listStack = new LinkedListStack<>();
        double t2 = testStack(listStack, opCount);
        System.out.println("listStack , time: " + t2 + " s");
    }

    private static double testStack(Stack<Integer> stack, int opCount) {
        long startTime = System.nanoTime();
        Random random = new Random();
        for (int i = 0; i < opCount; i++ ) {
            stack.push(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0; i < opCount; i++ ) {
            stack.pop();
        }
        long endTime = System.nanoTime();
        return (endTime - startTime) / 1000000000.0;
    }

}
